package com.quanxi.qxaimain.service.downloadExcelServer;

import com.quanxi.qxaimain.common.util.ExcelResultHandler.ExcelExportResultHandler;
import com.quanxi.qxaimain.common.util.execlExporttool.SXSSFWorkbookExporter;
import com.quanxi.qxaimain.dao.SDQuery.SDQueryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.OutputStream;
import java.util.*;

/**
 * @Author lyx
 * @create 2022/11/14 10:05
 */
@Slf4j
@Service("downloadExcelServer")
public class downloadExcelServerImpl implements downloadExcelServer {

    /**
     * 普通listMap导出excel服务接口
     *  @param listMap 数据库集
     *  @param fileName  excel名称
     *  @param outputStream 导出的数据流

     */

    @Override
    public void downloadExcel(List<Map<String, Object>> listMap, String fileName, OutputStream outputStream) {

        if(listMap.isEmpty() ||listMap==null){
            Map<String, Object> map =new HashMap<String, Object>();
            map.put(fileName,"无数据，请检查搜索条件");
            listMap.add(map);
        }

        Map<String, Object> map = listMap.get(0);
        Set<String> set = map.keySet();
        List<String> list = new ArrayList(set);
        String[] headname = new String[list.size()];
        list.toArray(headname ); //获取Excel列名


        /**
         * 将List<Map<String, Object>>写出到excel中
         */
        SXSSFWorkbookExporter hssfWorkExcel = new SXSSFWorkbookExporter( headname, fileName.replace(".xlsx",""));//初始化execl
        hssfWorkExcel.createTableRows(listMap, headname);//插入数据
        try {
            //   hssfWorkExcel.exportExcel(new FileOutputStream(new File("F:\\360MoveData\\Users\\lyx\\Desktop\\bigXmlConvertDB-master\\人员基本信息.xlsx"))); //导出数据
            hssfWorkExcel.exportExcel(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }





    /**
     * 大数据导出excel服务接口
     *  @param excelName excel名字
     *  @param sql  sql语句
     *  @param isExportZip 是否导出压缩zip
     *  @param ExcelSize excel 每个sheet的行数（零到一百万之间）
     */

    @Resource
    private SDQueryMapper SDQueryMapper;
    @Override
    public void downloadExcelSDQuery(String excelName,String sql,boolean isExportZip,int ExcelSize )   {


//
//        String sql = "SELECT  ORG_CODE,DOC_ADV_ID,INPATIENT_VISIT_FORM_NO,REVOKE_SIGN,DOC_ADV_DETAIL_NAME   FROM TB_CIS_DRADVICE_DETAIL where rownum<100";
//        String excelName="TB_CIS_DRADVICE_DETAIL";
       // String[] headerNames= new String []{"ORG_CODE","DOC_ADV_ID","INPATIENT_VISIT_FORM_NO","REVOKE_SIGN","DOC_ADV_DETAIL_NAME"};
   //     long timeMillis = System.currentTimeMillis();
        ExcelExportResultHandler< Map>  excelExportResultHandler = new ExcelExportResultHandler< Map>(excelName,isExportZip) {
            @Override
            public void tryFetchDataAndWriteToExcel() {
                SDQueryMapper.streamDataDynamicHandle(sql,this);
            }
        };
        excelExportResultHandler.setExcelSize(ExcelSize);

        excelExportResultHandler.exportExcel();

    //    System.out.println(System.currentTimeMillis() - timeMillis);

    }
}